package com.one.piece.service.impl;


import com.one.piece.document.Customer;
import com.one.piece.repository.CustomerRepository;
import com.one.piece.service.CustomerService;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(CustomerServiceImpl.class);

    @Override
    public Customer findCustomer(String id) {

        Customer customer = customerRepository.findOne(id);
        LOG.info(" get Customer by id {} is {}", id, customer);
        return customer;
    }


    @Override
    public Customer save(Customer customer) {
        Customer save = customerRepository.save(customer);

        LOG.info(" save Customer is {}", customer);
        return save;
    }

    @Override
    public void delete(String id) {
        customerRepository.delete(id);
    }


    @Override
    public List<Customer> findByFirstNameOrLastName(String firstName, String lastName) {
        return customerRepository.findByFirstNameOrLastName(firstName, lastName);
    }

    @Override
    public List<Customer> findByFirstNameAndLastName(String firstName, String lastName) {
        return customerRepository.findByFirstNameAndLastName(firstName, lastName);
    }

    @Override
    public Page<Customer> findAll(Pageable pageable) {
        return customerRepository.findAll(pageable);
    }

    @Override
    public Page<Customer> findAllSort(Pageable pageable) {
        pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(),new Sort(Sort.Direction.DESC, "id") );
        return customerRepository.findAll(pageable);
    }

    @Override
    public Page<Customer> findByFirstName(String firstName, Pageable pageable) {
        return customerRepository.findByFirstName(firstName, pageable);
    }

    @Override
    public Page<Customer> findByLastName(String lastName, Pageable pageable) {
        return customerRepository.findByLastName(lastName, pageable);
    }

    @Override
    public Page<Customer> findByFirstNameOrLastName(String firstName, String lastName, Pageable pageable) {
        return customerRepository.findByFirstNameOrLastName(firstName, lastName, pageable);
    }

    @Override
    public Page<Customer> findByFirstNameAndLastName(String firstName, String lastName, Pageable pageable) {
        return customerRepository.findByFirstNameAndLastName(firstName, lastName, pageable);
    }

    @Override
    public Page<Customer> findByAgeBetween(int from, int to, Pageable pageable) {
        return customerRepository.findByAgeBetween(from, to, pageable);
    }

    @Override
    public Page<Customer> findByFirstNameLike(String firstName, Pageable pageable) {
        return customerRepository.findByFirstNameLike(firstName, pageable);
    }

    @Override
    public Page<Customer> findByAddress(String address, Pageable pageable) {
        return customerRepository.findByAddress(address, pageable);
    }

    @Override
    public Page<Customer> search(String id, Pageable pageable) {
        return customerRepository.search(QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("id", id)), pageable);
    }


}